package com.vick.com;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import com.gen.tool.PlayerInfo;
import com.gen.tool.SoundPlayer;
import com.ouken.com.R;
import com.ouken.tool.ConnectServer;
import com.ouken.tool.MyApp;
import com.vick.tool.DialogTool;
import com.vick.tool.MiscTool;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.widget.TextView;

public class ChallengerwaitingActivity extends Activity {
	private Handler mHandler = new Handler();
	private long timeNow;
	private TextView tv_time,tv_wait1;
	CountDownTimer countDown=new CountDownTimer(120000, 1000) {
		public void onTick(long millisUntilFinished) {
			timeNow = millisUntilFinished / 1000;
			tv_time.setText(Long.toString(millisUntilFinished / 1000));
		}
		public void onFinish() {
			MiscTool.goToRoom(ChallengerwaitingActivity.this,null);
			ChallengerwaitingActivity.this.finish();
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.challengerwaiting);
		tv_time = (TextView) findViewById(R.id.challengerwaitingActivity_tv_time);
		
		tv_wait1 = (TextView) findViewById(R.id.challengerwaitingActivity_tv_waittext1);
		
		countDown.start();
		mHandler.removeCallbacks(waitDrawingTask);
		mHandler.postDelayed(waitDrawingTask, 2000);
		PlayerInfo provider=MiscTool.getProvider(this);
		tv_wait1.setText(provider.getNickName()+" is drawing");
	}
	

	@Override
	protected void onPause() {
		super.onPause();
		SoundPlayer.pause_c_waiting_music();
	}


	@Override
	protected void onResume() {	
		super.onResume();
		SoundPlayer.start_c_waiting_music();
		
	}
	@Override
	protected void onDestroy() {
		super.onDestroy();
		mHandler.removeCallbacks(waitDrawingTask);
		countDown.cancel();
	}


	Runnable waitDrawingTask = new Runnable() {

		@Override
		public void run() {
			List<BasicNameValuePair> connectParams = new ArrayList<BasicNameValuePair>();
			MyApp myApp = (MyApp) getApplicationContext();
			connectParams.add(new BasicNameValuePair("email", myApp.getUserEmail()));
			connectParams.add(new BasicNameValuePair("roomname", myApp.getRoomName()));
			connectParams.add(new BasicNameValuePair("MAC", myApp.getMac()));
			connectParams.add(new BasicNameValuePair("time", Long.toString(timeNow)));
			connectParams.add(new BasicNameValuePair("ParamEnd", "ParamEndParamEndParamEnd"));
			String result = ConnectServer.connect("http://"+ConnectServer.getServerAdd()+"/challenger/wait-drawing", connectParams);
			
			if (!DialogTool.checkNetworkAlive(result,
					ChallengerwaitingActivity.this,
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							mHandler.postDelayed(waitDrawingTask, 2000);

						}
					}))
				return;
			if(!DialogTool.checkLogin(result, ChallengerwaitingActivity.this))
				return;
			
			result=result.replace(" ", "");
			String[] resultParam = result.split(",");
			if ("OK".equals(resultParam[0])) {
				if ("finished".equals(resultParam[1])) {
					countDown.cancel();
					mHandler.removeCallbacks(waitDrawingTask);
					
					Intent intent = new Intent(ChallengerwaitingActivity.this, ChallengerplayingActivity.class);
					intent.putExtra("role", "challenger");
					startActivity(intent);
					//gen added
					SoundPlayer.stop_c_waiting_music();
					ChallengerwaitingActivity.this.finish();
					
				} else if ("waiting".equals(resultParam[1])) {
					mHandler.postDelayed(this, 2000);
				} else if ("reset".equals(resultParam[1])) {
					countDown.cancel();
					MiscTool.goToRoom(ChallengerwaitingActivity.this,resultParam[2]);
					ChallengerwaitingActivity.this.finish();
				}
			} else {
				new AlertDialog.Builder(ChallengerwaitingActivity.this).setTitle("Error").setMessage(result).setCancelable(false)
				.setNeutralButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						SoundPlayer.playSound(R.raw.keypress_common);
						dialog.cancel();
						mHandler.postDelayed(waitDrawingTask, 2000);
					}
				}).create().show();
			}

		}
	};
}
